< previous page page_398 next page >

Page 398
0398-01.gif
Figure 12.19
Rotating an object around its center.
e10a6a0a59f035e870d90cca05b85fce.gif
    Result(1, 1) = sx
    Result(2, 2) = sy
End Sub

' Create a rotation matrix for rotating by the
' given angle (in radians).
Public Sub m2Rotate(Result() As Single, ByVal theta As Single)
    m2Identity Result
    Result(1, 1) = Cos(theta)
    Result(1, 2) = Sin(theta)
    Result(2, 1) = -Result(1, 2)
    Result(2, 2) = Result(1, 1)
End Sub
Sometimes you will find it easier to specify a rotation with a point that should be rotated until it lies along the X or Y axis rather than specifying an angle of rotation. When you want to reflect across an arbitrary line, for instance, you need to rotate the line until it lies in the X axis. Specifying a point on the line is easier than calculating the angle through which the line must be rotated.
Figure 12.20 shows a point that should be rotated into the X axis. The distance d is the distance from the point to the origin and equals Sqr(x * x + y * y). Because q is measured in the negative direction, Cos(q) = x / d and Sin(q) = y / d. In that case, the rotation transformation matrix becomes:

 
< previous page page_398 next page >